package com.qingxin.wx.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.cms.entity.CmsArticle;
import com.qingxin.cms.service.CmsArticleService;
import com.qingxin.park.entity.BatchProducts;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.service.BatchProductsService;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.park.vo.TraceData;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.StringUtils;

/**
 * 微信小程序
 * @author Administrator
 *
 */
@Controller
public class WxappAction extends BaseAction {
	
	@Autowired
	private CmsArticleService cmsArticleService;
	@Autowired
	private FarmParkService farmParkService;
	@Autowired
	private BatchProductsService batchProductsService;
	
	
	/**
	 * 微信小程序接口
	 * @param method
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/wx/appdata.json", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Object mp_index(String method, Integer page, Integer pageNum, String id, HttpServletRequest request){
		if(StringUtils.isEmpty(method)){
			return "Error-0";
		}
		
		switch (method) {
		case "GetRollAdvList"://获取滚动内容
			return loadGetRollAdvList(request);
			
		case "GetParkListByPage"://分页获取园区
			return loadGetParkListByPage(page, pageNum, request);
			
		case "GetParkInfoByID"://根据园区ID获取详情
			return loadGetParkInfoByID(id, request);
			
		case "GeTop3ParkActivityByID"://根据园区ID获取Top3的园区活动
			return loadGeTop3ParkActivityByID(id, request);
			
		case "GeTop3ParkMienByID"://根据园区ID获取Top3的园区风采
			return loadGeTop3ParkMienByID(id, request);
			
		case "GetParkActivityListByPage"://分页获取园区活动
			return loadGetParkActivityListByPage(id, page, pageNum, request);
			
		case "GetParkMeinListByPage"://分页获取园区风采
			return loadGetParkMeinListByPage(id, page, pageNum, request);
			
		case "GetParkActivityByID"://获取园区活动详情
		{
			String actid = request.getParameter("actid");
			return loadArticle(actid, request);
		}
		case "GetParkMeinByID"://获取园区风采详情
		{
			String meinid = request.getParameter("meinid");
			return loadArticle(meinid, request);
		}
		case "GetBackByCode"://追溯查询
		{
			String code = request.getParameter("code");
			return loadGetBackByCode(code, request);
		}
		}
		
		return "Error-1";
	}
	
	
	//获取滚动内容
	public Object loadGetRollAdvList(HttpServletRequest request){
		List<Map<String, String>> list = new ArrayList<>();
		List<CmsArticle> articles = cmsArticleService.listCarousel();
		if(articles != null){
			for(CmsArticle article : articles){
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", article.getArticleId());
				map.put("title", article.getArticleTitle());
				map.put("cate", article.getCmsCategory().getCategoryCode());
				map.put("img", UploadFile.getFileUrl(UploadFile.DIR_NEWS_IMG, article.getThumb()));
				
				list.add(map);
			}
		}
		return list;
	}
	
	//分页获取园区
	@SuppressWarnings("unchecked")
	public Object loadGetParkListByPage(Integer page, Integer pageNum, HttpServletRequest request){
		List<Map<String, String>> list = new ArrayList<>();
		PageParam pageParam = new PageParam();
		pageParam.setPage(page);
		pageParam.setPagesize(pageNum);
		Pager pager = farmParkService.listAllPager(pageParam);
		if(pager != null && pager.getList() != null){
			List<FarmPark> parks = (List<FarmPark>) pager.getList();
			for(FarmPark park : parks){
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", park.getParkid());
				map.put("name", park.getParkName());
				map.put("img", UploadFile.getFileUrl(UploadFile.DIR_FARM_IMG, park.getLogoUrl()));
				map.put("address", park.getParkAddress());
				map.put("remark", park.getRemark());
				map.put("level", ""+park.getParkLevel());
				
				list.add(map);
			}
		}
		return list;
	}
	
	
	//根据园区ID获取详情
	public Object loadGetParkInfoByID(String id, HttpServletRequest request){
		Map<String, String> map = new HashMap<String, String>();
		FarmPark park = farmParkService.get(id);
		if(park != null){
			map.put("id", park.getParkid());
			map.put("name", park.getParkName());
			map.put("img", UploadFile.getFileUrl(UploadFile.DIR_FARM_IMG, park.getLogoUrl()));
			map.put("address", park.getParkAddress());
			map.put("remark", park.getRemark());
			map.put("level", ""+park.getParkLevel());
			map.put("person", park.getContactPerson());
			map.put("phone", park.getPhone());
			map.put("mobile", park.getMobile());
			map.put("email", park.getEmail());
			map.put("wechat", park.getWechat());
			map.put("lat", ""+park.getLat());
			map.put("lng", ""+park.getLng());
			map.put("describ", park.getDescrib());
		}
		return map;
	}
	
	
	//根据园区ID获取Top3的园区活动
	public Object loadGeTop3ParkActivityByID(String parkid, HttpServletRequest request){
		List<Map<String, String>> list = new ArrayList<>();
		List<CmsArticle> articles = cmsArticleService.listByPark("yuanquhuodong", 3, parkid);
		if(articles != null){
			for(CmsArticle article : articles){
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", article.getArticleId());
				map.put("title", article.getArticleTitle());
				map.put("img", UploadFile.getFileUrl(UploadFile.DIR_NEWS_IMG, article.getThumb()));
				map.put("desc", article.getArticleDesc());
				
				list.add(map);
			}
		}
		return list;
	}
	
	//根据园区ID获取Top3的园区风采
	public Object loadGeTop3ParkMienByID(String parkid, HttpServletRequest request){
		List<Map<String, String>> list = new ArrayList<>();
		List<CmsArticle> articles = cmsArticleService.listByPark("yuanqufengcai", 3, parkid);
		if(articles != null){
			for(CmsArticle article : articles){
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", article.getArticleId());
				map.put("title", article.getArticleTitle());
				map.put("img", UploadFile.getFileUrl(UploadFile.DIR_NEWS_IMG, article.getThumb()));
				map.put("desc", article.getArticleDesc());
				
				list.add(map);
			}
		}
		return list;
	}
	
	
	//分页获取园区活动
	@SuppressWarnings("unchecked")
	public Object loadGetParkActivityListByPage(String parkid, Integer page, Integer pageNum, HttpServletRequest request){
		List<Map<String, String>> list = new ArrayList<>();
		PageParam pageParam = new PageParam();
		pageParam.setPage(page);
		pageParam.setPagesize(pageNum);
		Pager pager = cmsArticleService.listByPark(pageParam, "yuanquhuodong", parkid, request);
		if(pager != null && pager.getList() != null){
			List<CmsArticle> articles = (List<CmsArticle>) pager.getList();
			for(CmsArticle article : articles){
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", article.getArticleId());
				map.put("title", article.getArticleTitle());
				map.put("img", UploadFile.getFileUrl(UploadFile.DIR_NEWS_IMG, article.getThumb()));
				map.put("desc", article.getArticleDesc());
				
				list.add(map);
			}
		}
		return list;
	}
	
	
	//分页获取园区风采
	@SuppressWarnings("unchecked")
	public Object loadGetParkMeinListByPage(String parkid, Integer page, Integer pageNum, HttpServletRequest request){
		List<Map<String, String>> list = new ArrayList<>();
		PageParam pageParam = new PageParam();
		pageParam.setPage(page);
		pageParam.setPagesize(pageNum);
		Pager pager = cmsArticleService.listByPark(pageParam, "yuanqufengcai", parkid, request);
		if(pager != null && pager.getList() != null){
			List<CmsArticle> articles = (List<CmsArticle>) pager.getList();
			for(CmsArticle article : articles){
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", article.getArticleId());
				map.put("title", article.getArticleTitle());
				map.put("img", UploadFile.getFileUrl(UploadFile.DIR_NEWS_IMG, article.getThumb()));
				map.put("desc", article.getArticleDesc());
				
				list.add(map);
			}
		}
		return list;
	}
	
	
	//获取文章内容
	public Object loadArticle(String articleId, HttpServletRequest request){
		Map<String, String> map = new HashMap<String, String>();
		CmsArticle article = cmsArticleService.get(articleId);
		if(article != null){
			map.put("id", article.getArticleId());
			map.put("title", article.getArticleTitle());
			map.put("img", UploadFile.getFileUrl(UploadFile.DIR_NEWS_IMG, article.getThumb()));
			map.put("desc", article.getArticleDesc());
			map.put("keyword", article.getKeyWord());
			map.put("content", article.getArticleContent());
		}
		return map;
	}
	
	
	//追溯查询
	public Object loadGetBackByCode(String code, HttpServletRequest request){
		if(StringUtils.isEmpty(code)){
			BatchProducts products = batchProductsService.getLastestProducts();
			if(products == null){
				return new TraceData(404, "没有找到相关信息", 1);
			}
			code = products.getPbn();
		}
		return new TraceData(404, "没有找到相关信息", 1);
//		TraceData traceData = batchProductsService.getTraceDataAbsImg(code, true);
//		return traceData;
	}
	
	
	
	
}
